package com.zhixuan.autoui.sample;

import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

import com.tinker.core.misc.gain.GainCategory;
import com.tinker.core.misc.volume.VolumeCategory;
import com.tinker.coresdk.manager.ZXSoundEffectManager;
import com.tinker.coresdk.release.iface.SoundEffectTool;
import com.tinker.pojo.EqualizerConfig;
import com.zhixuan.autoui.R;

import java.util.Arrays;

public class VolumeSettingsActivity extends BaseTestActivity {

    private SeekBar sbar;

    @Override
    protected int onContentLayout() {
        return R.layout.sample_activity_settings_volume;
    }

    @Override
    protected void onCreated(Bundle savedInstanceState) {

        sbar = findViewById(R.id.sbar);
        sbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                try {
                    ZXSoundEffectManager.getInstance().requestSetVolume(category[categoryIndex], progress);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        ZXSoundEffectManager.getInstance().setTool(new SoundEffectTool() {
            @Override
            public void onSystemVolumeChangeByUser(String s, String s1, int i) {
                addMessage("onSystemVolumeChangeByUser:" + s + "," + s1 + "," + i);
            }

            @Override
            public void onVolumeCategoryConfigFinish(VolumeCategory volumeCategory) {
                addMessage("onVolumeCategoryConfigFinish:" + volumeCategory);

            }

            @Override
            public void onGainCategoryConfigFinish(GainCategory gainCategory) {
                addMessage("onGainCategoryConfigFinish:" + gainCategory);

            }

            @Override
            public void onEqualizerConfigFinish(EqualizerConfig equalizerConfig) {
                addMessage("onEqualizerConfigFinish:" + equalizerConfig);

            }

            @Override
            public void onSystemVolumeChange(String s, String s1, int i) {
                addMessage("onSystemVolumeChange:" + s + "," + s1 + "," + i);

            }

            @Override
            public void onVolumeGainChange(String s, String s1, int i) {
                addMessage("onVolumeGainChange:" + s + "," + s1 + "," + i);

            }

            @Override
            public void onSystemMuteChange(boolean b) {
                addMessage("onSystemMuteChange:" + b);

            }

            @Override
            public void onVolumeCategoryMuteChange(String s, boolean b) {
                addMessage("onVolumeCategoryMuteChange:" + s + "," + b);

            }

            @Override
            public void onSetSoundField(byte b, byte b1) {
                addMessage("onSetSoundField:" + b + "," + b1);

            }

            @Override
            public void onSetSoundEffect(byte b, byte b1, byte b2, byte b3, byte b4, byte b5) {
                addMessage("onSetSoundEffect:" + b + "," + b1 + "," + b2 + "," + b3 + "," + b4 + "," + b5);

            }

            @Override
            public void onSetSoundEffectEQ(byte b, byte[] bytes) {
                addMessage("onSetSoundEffectEQ:" + b + "," + Arrays.toString(bytes));

            }

            @Override
            public void onPresetsForEqualizer(int i) {
                addMessage("onPresetsForEqualizer:" + i);

            }

            @Override
            public void onSetSubwooferGain(int i) {
                addMessage("onSetSubwooferGain:" + i);

            }

            @Override
            public void onSetSubwooferGainPower(boolean b) {
                addMessage("onSetSubwooferGainPower:" + b);

            }

            @Override
            public void onSetSubwooferLPF(int i) {
                addMessage("onSetSubwooferLPF:" + i);

            }

            @Override
            public void onSetSubwooferPhase(int i) {
                addMessage("onSetSubwooferPhase:" + i);

            }

            @Override
            public void onSetNaviMixerType(int i) {
                addMessage("onSetNaviMixerType:" + i);

            }

            @Override
            public void onSetNaviVoiceChannel(int i) {
                addMessage("onSetNaviVoiceChannel:" + i);

            }

            @Override
            public void onSetBeep(boolean b) {
                addMessage("onSetBeep:" + b);

            }

            @Override
            public void onSetLoudnessSwitcher(boolean b) {
                addMessage("onSetLoudnessSwitcher:" + b);

            }

            @Override
            public void onSetLoudnessLevel(int i) {
                addMessage("onSetLoudnessLevel:" + i);

            }

            @Override
            public void onSetBTVoiceChannelMode(int i) {
                addMessage("onSetBTVoiceChannelMode:" + i);

            }

            @Override
            public void onSetNaviChannel(int i) {
                addMessage("onSetNaviChannel:" + i);

            }

            @Override
            public void onRequestSetVolumeByCategory(String s, int i) {
                addMessage("onRequestSetVolumeByCategory:" + s + "," + i);

            }

            @Override
            public void onRequestSetVolumeUIByCategory(String s, int i, boolean b) {
                addMessage("onRequestSetVolumeUIByCategory:" + s + "," + i + "," + b);

            }

        });
        setCategory();
    }

    public void onGetVolumes(View view) {
        int[] volumes;
        for (VolumeCategory.Type cat : category) {
            try {
                volumes = ZXSoundEffectManager.getInstance().getVolumes(0, cat);
                addMessage("获取" + cat + "音量：" + Arrays.toString(volumes));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private final VolumeCategory.Type[] category = VolumeCategory.Type.values();

    private int categoryIndex = 0;

    public void onSetCategory(View view) {
        setCategory();
    }

    private void setCategory() {
        categoryIndex++;
        categoryIndex %= category.length;
        try {
            int[] volumes = ZXSoundEffectManager.getInstance().getVolumes(0, category[categoryIndex]);
            sbar.setProgress(volumes[0]);
            sbar.setMin(volumes[1]);
            sbar.setMax(volumes[2]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        addMessage("切换音量类别：" + category[categoryIndex]);
    }


    public void onIncrease(View view) {
        try {
            ZXSoundEffectManager.getInstance().requestStepVolume(category[categoryIndex], true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onDecrease(View view) {
        try {
            ZXSoundEffectManager.getInstance().requestStepVolume(category[categoryIndex], false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}